package com.itheima.web.admin;

import com.itheima.model.domain.Article;
import com.itheima.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chenxiangxi
 * @create 2023/6/7 23:41
 */
@Controller
public class SearchController {

//    @Autowired
//    private IArticleService articleServiceImpl;
//
//
//    @GetMapping("/newSearch")
//    public String search(@RequestParam String keyword) {
//        List<Article> articles = articleServiceImpl.findByTitleContainingOrContentContaining(keyword);
//        StringBuilder sb = new StringBuilder();
//        for (Article article : articles) {
//            int index = Math.max(article.getTitle().indexOf(keyword), article.getContent().indexOf(keyword));
//            String snippet = index < 50 ? article.getContent().substring(0, Math.min(50, article.getContent().length())) : article.getContent().substring(index - 50, Math.min(index + 50, article.getContent().length()));
//            snippet = snippet.replaceAll(keyword, "<span style=\"color:red;\">" + keyword + "</span>");
//            sb.append("<div>")
//                    .append("<a href=\"localhost:8083/search")
//                    .append(article.getId())
//                    .append("\">")
//                    .append("<h3>")
//                    .append(article.getTitle().replaceAll(keyword, "<span style=\"color:red;\">" + keyword + "</span>"))
//                    .append("</h3>")
//                    .append("</a>")
//                    .append("<p>").append(snippet).append("...</p>")
//                    .append("</div>")
//                    .append("<hr>");
//        }
//        return sb.toString();
//    }
//}
}